package com.love.cloud.common.core.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

/**
 * Description: <br/>
 * date: 2022/5/16 11:15<br/>
 *
 * @author wangzhiqiong <br/>
 */
@Slf4j
public class MyDateUtil {

    public static LocalDateTime parse(String dateStr) {
        if(StringUtils.isBlank(dateStr)) {
            return null;
        }
        try {
            int size = dateStr.trim().length();
            if(DatePattern.NORM_DATE_PATTERN.length() == size) {
                return LocalDateTimeUtil.parse(dateStr, DatePattern.NORM_DATE_PATTERN);
            }
            if(DatePattern.NORM_DATETIME_PATTERN.length() == size) {
                return LocalDateTimeUtil.parse(dateStr, DatePattern.NORM_DATETIME_PATTERN);
            }
            if(DatePattern.NORM_DATETIME_MINUTE_PATTERN.length() == size) {
                return LocalDateTimeUtil.parse(dateStr, DatePattern.NORM_DATETIME_MINUTE_PATTERN);
            }
        } catch (Exception e) {
            log.error("解析日期出错dateStr={}",dateStr, e);
        }
        return null;
    }
}
